package com.haidong.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 主要描述通知的各个位置起的不同作用
 */
@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(* com.haidong.dao.BookDao.*())")
    private void pt(){}

    @Before("pt()")
    public void before(){
        System.out.println("before advice");
    }

    @After("pt()")
    public void after(){
        System.out.println("after advice");
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around before advice");

        // 带有返回值的方法,环绕通知要获取原方法的返回值,void就是返回null
        Object ret = pjp.proceed();

        System.out.println("around after advice");
        return ret;
    }

    @AfterReturning("pt()")
    public void afterReturning(){
        System.out.println("afterReturning advice");
    }

    @AfterThrowing("pt()")
    public void afterThrowing(){
        System.out.println("afterThrowing advice");
    }
}
